
* set working dir

cd XXXX
set more off

/* --------------------- CREATE THE INDIVIDUAL VARIABLES ---------------------*/
/*
These are the variables we create, one each for Q: 



q1r_dominated : value 1 if the agent doesn't have a 10 in the first line or a 0 in the last; 0 otherwise

For subjects with dominated values, we don't compute any other measures. For the others, 

q1r_64_norange : value 1 if the agent doesn't have a 6-4 range; Example: 10, 7, 3, 3; or 10, 7, 5, 3.
q1r_82_norange : same as above but 8-2
q1r_91_norange : same as above but 9-1

Notice that a condition for having a range is to report something within the range (e.g., in [1, 9]) at least twice.

For subjects with a range, then we have:

q1r_91min : first time we observe a choice in [1, 9] 
q1r_91max : last time we observe a choice in [1, 9]; below it is either 0 or 10s, but never in [1, 9]

q1r_82min, q1r_82max, 73, 64, 55, are defined in the same way.


For subjects without a range, then we have:

For each of the ranges above (91, 82, 73, 64), if their answers are: 
	- 10, 5, 0, then the value of l1r_91min = l1r_91max is where they report 5 (row 2 in this case)
	- 10, 0, 0, then the value of l1r_91min = l1r_91max is last time they have a 10 (row 1 in this case); the ranges 82 and 64 follow a similar logic.

*/
use "cleaned.dta", clear

*****************************************
*****  		RANGE QUESTIONS       *******
*****************************************

local rangequestions "q5r q6r q7r"
 
foreach q of local rangequestions{

	* set the correct number of rows per question

	if  "`q'" =="q5r" local nrows "19"
	if  "`q'" =="q6r" local nrows "23"
	if  "`q'" =="q7r" local nrows "17"


	gen `q'_nonMonotoneRange=0

	gen `q'_91min=.
	gen `q'_91max=.
	gen `q'_91mi_range=.
	gen `q'_91ma_range=.
	gen `q'_91_norange=0

	gen `q'_82min=.
	gen `q'_82max=.
	gen `q'_82mi_range=.
	gen `q'_82ma_range=.
	gen `q'_82_norange=0
	
	gen `q'_73min=.
	gen `q'_73max=.
	gen `q'_73mi_range=.
	gen `q'_73ma_range=.
	gen `q'_73_norange=0
	
	gen `q'_64min=.
	gen `q'_64max=.
	gen `q'_64mi_range=.
	gen `q'_64ma_range=.
	gen `q'_64_norange=0
	
	gen `q'_55min=.
	gen `q'_55max=.
	gen `q'_55mi_range=.
	gen `q'_55ma_range=.
	gen `q'_55_norange=0
	
	gen `q'_dominated = 0

	

	* Dominated choices:

		* Mark as dominated if the choice in row 1 is not equal to 1 for questions 1-6
		if "`q'" != "q7r" replace `q'_dominated = 1 if choice`q'1 != 10

		* Or if the choice in the last row is dominated for questions 5 and 7
		if "`q'" == "q5r" | "`q'" == "q7r" replace `q'_dominated = 1 if choice`q'`nrows'!= 0
	
	forvalue i =1(1)`nrows'{ 

		replace `q'_91min = `i' if choice`q'`i' <= 9 & `q'_91min ==. & `q'_dominated == 0
		replace `q'_91max = `i' if choice`q'`i' >= 1 & `q'_dominated == 0
		replace `q'_91mi_range = `i' if choice`q'`i' >= 1 & choice`q'`i' <= 9  & `q'_91mi_range ==. & `q'_dominated == 0
		replace `q'_91ma_range = `i' if choice`q'`i' >= 1 & choice`q'`i' <= 9 & `q'_dominated == 0

		replace `q'_82min = `i' if choice`q'`i' <= 8 & `q'_82min ==. & `q'_dominated == 0
		replace `q'_82max = `i' if choice`q'`i' >= 2 & `q'_dominated == 0
		replace `q'_82mi_range = `i' if choice`q'`i' >= 2 & choice`q'`i' <= 8  & `q'_82mi_range ==. & `q'_dominated == 0
		replace `q'_82ma_range = `i' if choice`q'`i' >= 2 & choice`q'`i' <= 8 & `q'_dominated == 0

		replace `q'_73min = `i' if choice`q'`i' <= 7 & `q'_73min ==. & `q'_dominated == 0
		replace `q'_73max = `i' if choice`q'`i' >= 3 & `q'_dominated == 0
		replace `q'_73mi_range = `i' if choice`q'`i' >= 3 & choice`q'`i' <= 7  & `q'_73mi_range ==. & `q'_dominated == 0
		replace `q'_73ma_range = `i' if choice`q'`i' >= 3 & choice`q'`i' <= 7 & `q'_dominated == 0
		
		replace `q'_64min = `i' if choice`q'`i' <= 6 & `q'_64min ==. & `q'_dominated == 0
		replace `q'_64max = `i' if choice`q'`i' >= 4 & `q'_dominated == 0
		replace `q'_64mi_range = `i' if choice`q'`i' >= 4 & choice`q'`i' <= 6  & `q'_64mi_range ==. & `q'_dominated == 0
		replace `q'_64ma_range = `i' if choice`q'`i' >= 4 & choice`q'`i' <= 6 & `q'_dominated == 0
		
		replace `q'_55min = `i' if choice`q'`i' <= 5 & `q'_55min ==. & `q'_dominated == 0
		replace `q'_55max = `i' if choice`q'`i' >= 5 & `q'_dominated == 0
		replace `q'_55mi_range = `i' if choice`q'`i' >= 5 & choice`q'`i' <= 5  & `q'_55mi_range ==. & `q'_dominated == 0
		replace `q'_55ma_range = `i' if choice`q'`i' >= 5 & choice`q'`i' <= 5 & `q'_dominated == 0

		
		if `i' > 1  replace `q'_nonMonotoneRange=1 if choice`q'`i' > choice`q'`--i'
		
	}


	
	*Start with range91 then do the rest
	
	* For debugging let's create these values; we should never have them.
	replace `q'_91_norange = 7 if `q'_91mi_range > `q'_91ma_range & `q'_91mi_range != . & `q'_91ma_range != . & `q'_dominated == 0
	replace `q'_91_norange = 8 if `q'_91min > `q'_91max & `q'_91min != . & `q'_91max != . & `q'_dominated == 0

	
	*replace `q'_91min = `q'_91max if `q'_91min > `q'_91max & `q'_dominated == 0
	replace `q'_91_norange = 1 if `q'_91min == `q'_91max & `q'_dominated == 0
	
	replace `q'_91_norange = 1 if `q'_91mi_range == `q'_91ma_range & `q'_dominated == 0
	replace `q'_91_norange = 1 if `q'_91mi_range == . & `q'_dominated == 0
	replace `q'_91_norange = 1 if `q'_91ma_range == . & `q'_dominated == 0
	
	
	* For subjects with a range, we define the min and the max of the range using the boundaries of where they give values in [1, 9]
	replace `q'_91min = `q'_91mi_range if `q'_91_norange == 0
	replace `q'_91max = `q'_91ma_range if `q'_91_norange == 0

	
	* Now 82
	
	* For debugging let's create these values; we should never have them.
	replace `q'_82_norange = 7 if `q'_82mi_range > `q'_82ma_range & `q'_82mi_range != . & `q'_82ma_range != . & `q'_dominated == 0
	replace `q'_82_norange = 8 if `q'_82min > `q'_82max & `q'_82min != . & `q'_82max != . & `q'_dominated == 0

	
	*replace `q'_82min = `q'_82max if `q'_82min > `q'_82max & `q'_dominated == 0
	replace `q'_82_norange = 1 if `q'_82min == `q'_82max & `q'_dominated == 0
	
	replace `q'_82_norange = 1 if `q'_82mi_range == `q'_82ma_range & `q'_dominated == 0
	replace `q'_82_norange = 1 if `q'_82mi_range == . & `q'_dominated == 0
	replace `q'_82_norange = 1 if `q'_82ma_range == . & `q'_dominated == 0
	
	
	* For subjects with a range, we define the min and the max of the range using the boundaries of where they give values in [1, 9]
	replace `q'_82min = `q'_82mi_range if `q'_82_norange == 0
	replace `q'_82max = `q'_82ma_range if `q'_82_norange == 0
	
	
* Now 73
	
	* For debugging let's create these values; we should never have them.
	replace `q'_73_norange = 7 if `q'_73mi_range > `q'_73ma_range & `q'_73mi_range != . & `q'_73ma_range != . & `q'_dominated == 0
	replace `q'_73_norange = 8 if `q'_73min > `q'_73max & `q'_73min != . & `q'_73max != . & `q'_dominated == 0

	
	*replace `q'_73min = `q'_73max if `q'_73min > `q'_73max & `q'_dominated == 0
	replace `q'_73_norange = 1 if `q'_73min == `q'_73max & `q'_dominated == 0
	
	replace `q'_73_norange = 1 if `q'_73mi_range == `q'_73ma_range & `q'_dominated == 0
	replace `q'_73_norange = 1 if `q'_73mi_range == . & `q'_dominated == 0
	replace `q'_73_norange = 1 if `q'_73ma_range == . & `q'_dominated == 0
	
	
	* For subjects with a range, we define the min and the max of the range using the boundaries of where they give values in [1, 9]
	replace `q'_73min = `q'_73mi_range if `q'_73_norange == 0
	replace `q'_73max = `q'_73ma_range if `q'_73_norange == 0
	
* Now 64
	
	* For debugging let's create these values; we should never have them.
	replace `q'_64_norange = 7 if `q'_64mi_range > `q'_64ma_range & `q'_64mi_range != . & `q'_64ma_range != . & `q'_dominated == 0
	replace `q'_64_norange = 8 if `q'_64min > `q'_64max & `q'_64min != . & `q'_64max != . & `q'_dominated == 0

	
	*replace `q'_64min = `q'_64max if `q'_64min > `q'_64max & `q'_dominated == 0
	replace `q'_64_norange = 1 if `q'_64min == `q'_64max & `q'_dominated == 0
	
	replace `q'_64_norange = 1 if `q'_64mi_range == `q'_64ma_range & `q'_dominated == 0
	replace `q'_64_norange = 1 if `q'_64mi_range == . & `q'_dominated == 0
	replace `q'_64_norange = 1 if `q'_64ma_range == . & `q'_dominated == 0
	
	
	* For subjects with a range, we define the min and the max of the range using the boundaries of where they give values in [1, 9]
	replace `q'_64min = `q'_64mi_range if `q'_64_norange == 0
	replace `q'_64max = `q'_64ma_range if `q'_64_norange == 0

* Now 55
	
	* For debugging let's create these values; we should never have them.
	replace `q'_55_norange = 7 if `q'_55mi_range > `q'_55ma_range & `q'_55mi_range != . & `q'_55ma_range != . & `q'_dominated == 0
	replace `q'_55_norange = 8 if `q'_55min > `q'_55max & `q'_55min != . & `q'_55max != . & `q'_dominated == 0

	
	*replace `q'_55min = `q'_55max if `q'_55min > `q'_55max & `q'_dominated == 0
	replace `q'_55_norange = 1 if `q'_55min == `q'_55max & `q'_dominated == 0
	
	replace `q'_55_norange = 1 if `q'_55mi_range == `q'_55ma_range & `q'_dominated == 0
	replace `q'_55_norange = 1 if `q'_55mi_range == . & `q'_dominated == 0
	replace `q'_55_norange = 1 if `q'_55ma_range == . & `q'_dominated == 0
	
	
	* For subjects with a range, we define the min and the max of the range using the boundaries of where they give values in [1, 9]
	replace `q'_55min = `q'_55mi_range if `q'_55_norange == 0
	replace `q'_55max = `q'_55ma_range if `q'_55_norange == 0

	
}



* ----- CONVERT TO DOLLAR VALUES   ----

* Now we convert all this to $$ values. We have to set the grid correctly */


foreach q of local rangequestions {

	gen `q'_range91_min_DollAv=.
	gen `q'_range91_min_DollTop=.
	gen `q'_range91_min_DollBottom=.
	
	gen `q'_range91_max_DollAv=.
	gen `q'_range91_max_DollTop=.
	gen `q'_range91_max_DollBottom=.
	
	gen `q'_range82_min_DollAv=.
	gen `q'_range82_min_DollTop=.
	gen `q'_range82_min_DollBottom=.
	
	gen `q'_range82_max_DollAv=.
	gen `q'_range82_max_DollTop=.
	gen `q'_range82_max_DollBottom=.
		

	gen `q'_range73_min_DollAv=.
	gen `q'_range73_min_DollTop=.
	gen `q'_range73_min_DollBottom=.
	
	gen `q'_range73_max_DollAv=.
	gen `q'_range73_max_DollTop=.
	gen `q'_range73_max_DollBottom=.
	
	gen `q'_range64_min_DollAv=.
	gen `q'_range64_min_DollTop=.
	gen `q'_range64_min_DollBottom=.
	
	gen `q'_range64_max_DollAv=.
	gen `q'_range64_max_DollTop=.
	gen `q'_range64_max_DollBottom=.
	
	gen `q'_range55_min_DollAv=.
	gen `q'_range55_min_DollTop=.
	gen `q'_range55_min_DollBottom=.
	
	gen `q'_range55_max_DollAv=.
	gen `q'_range55_max_DollTop=.
	gen `q'_range55_max_DollBottom=.


	
	* We identify the dollar grid per question. The list of questions is
		
	if  "`q'" =="q5r" local dollarGrid "0 4 6 7 7.5 8 8.5 9 9.5 10 10.5 11 11.5 12 12.5 13 14 16 20"
	if  "`q'" =="q6r" local dollarGrid "18 21 24 27 30 33 33.50 34 34.50 35 35.50 36 36.50 37 37.50 38 38.50 39 42 45 48 51 54"
	if  "`q'" =="q7r" local dollarGrid "14 14.5 15 15.5 16 16.5 17 17.5 18 18.5 19 19.5 20 20.5 21 21.5 22"
	
	
	local j = 0
	
	foreach i of local dollarGrid { 

	
		* i denotes the dollar value at that row; j is the counter that indicates the row#.
		*We store the previous value of i in prevValue; this is needed to compute the average.
		
		*If you switch at row j, acquire the value i as top value, the value prevValue for your bottom, and the average for average;
		
		*This is the rule unless you switch already in the first row. In that case we set all the values at the value of the first row.
			

		local j = `j'+1
		
		if `j'==1 {
		
			replace `q'_range91_min_DollBottom= `i' if `q'_91min==`j'
			replace `q'_range91_min_DollTop= `i' if `q'_91min==`j'
			replace `q'_range91_min_DollAv= `i' if `q'_91min==`j'
			
			
			replace `q'_range91_max_DollBottom= `i' if `q'_91max==`j'
			replace `q'_range91_max_DollTop= `i' if `q'_91max==`j'
			replace `q'_range91_max_DollAv= `i' if `q'_91max==`j'
			
			
			replace `q'_range82_min_DollBottom= `i' if `q'_82min==`j'
			replace `q'_range82_min_DollTop= `i' if `q'_82min==`j'
			replace `q'_range82_min_DollAv= `i' if `q'_82min==`j'
			
			
			replace `q'_range82_max_DollBottom= `i' if `q'_82max==`j'
			replace `q'_range82_max_DollTop= `i' if `q'_82max==`j'
			replace `q'_range82_max_DollAv= `i' if `q'_82max==`j'
			
			replace `q'_range73_min_DollBottom= `i' if `q'_73min==`j'
			replace `q'_range73_min_DollTop= `i' if `q'_73min==`j'
			replace `q'_range73_min_DollAv= `i' if `q'_73min==`j'
			
			
			replace `q'_range73_max_DollBottom= `i' if `q'_73max==`j'
			replace `q'_range73_max_DollTop= `i' if `q'_73max==`j'
			replace `q'_range73_max_DollAv= `i' if `q'_73max==`j'
			
			replace `q'_range64_min_DollBottom= `i' if `q'_64min==`j'
			replace `q'_range64_min_DollTop= `i' if `q'_64min==`j'
			replace `q'_range64_min_DollAv= `i' if `q'_64min==`j'
			
			
			replace `q'_range64_max_DollBottom= `i' if `q'_64max==`j'
			replace `q'_range64_max_DollTop= `i' if `q'_64max==`j'
			replace `q'_range64_max_DollAv= `i' if `q'_64max==`j'
			
			replace `q'_range55_min_DollBottom= `i' if `q'_55min==`j'
			replace `q'_range55_min_DollTop= `i' if `q'_55min==`j'
			replace `q'_range55_min_DollAv= `i' if `q'_55min==`j'
			
			
			replace `q'_range55_max_DollBottom= `i' if `q'_55max==`j'
			replace `q'_range55_max_DollTop= `i' if `q'_55max==`j'
			replace `q'_range55_max_DollAv= `i' if `q'_55max==`j'
			

		}
		
		
		if `j' > 1 {
		
		replace `q'_range91_min_DollBottom= `prevValue' if `q'_91min==`j'
		replace `q'_range91_min_DollTop= `i' if `q'_91min==`j'
		replace `q'_range91_min_DollAv= (`prevValue'+`i')/2 if `q'_91min==`j'
		
		
		replace `q'_range91_max_DollBottom= `prevValue' if `q'_91max==`j'
		replace `q'_range91_max_DollTop= `i' if `q'_91max==`j'
		replace `q'_range91_max_DollAv= (`prevValue'+`i')/2 if `q'_91max==`j'
		
		
		replace `q'_range82_min_DollBottom= `prevValue' if `q'_82min==`j'
		replace `q'_range82_min_DollTop= `i' if `q'_82min==`j'
		replace `q'_range82_min_DollAv= (`prevValue'+`i')/2 if `q'_82min==`j'
		
		
		replace `q'_range82_max_DollBottom= `prevValue' if `q'_82max==`j'
		replace `q'_range82_max_DollTop= `i' if `q'_82max==`j'
		replace `q'_range82_max_DollAv= (`prevValue'+`i')/2 if `q'_82max==`j'
		
		replace `q'_range73_min_DollBottom= `prevValue' if `q'_73min==`j'
		replace `q'_range73_min_DollTop= `i' if `q'_73min==`j'
		replace `q'_range73_min_DollAv= (`prevValue'+`i')/2 if `q'_73min==`j'
		
		
		replace `q'_range73_max_DollBottom= `prevValue' if `q'_73max==`j'
		replace `q'_range73_max_DollTop= `i' if `q'_73max==`j'
		replace `q'_range73_max_DollAv= (`prevValue'+`i')/2 if `q'_73max==`j'
		
		
		replace `q'_range64_min_DollBottom= `prevValue' if `q'_64min==`j'
		replace `q'_range64_min_DollTop= `i' if `q'_64min==`j'
		replace `q'_range64_min_DollAv= (`prevValue'+`i')/2 if `q'_64min==`j'
		
		
		replace `q'_range64_max_DollBottom= `prevValue' if `q'_64max==`j'
		replace `q'_range64_max_DollTop= `i' if `q'_64max==`j'
		replace `q'_range64_max_DollAv= (`prevValue'+`i')/2 if `q'_64max==`j'
		
		replace `q'_range55_min_DollBottom= `prevValue' if `q'_55min==`j'
		replace `q'_range55_min_DollTop= `i' if `q'_55min==`j'
		replace `q'_range55_min_DollAv= (`prevValue'+`i')/2 if `q'_55min==`j'
		
		
		replace `q'_range55_max_DollBottom= `prevValue' if `q'_55max==`j'
		replace `q'_range55_max_DollTop= `i' if `q'_55max==`j'
		replace `q'_range55_max_DollAv= (`prevValue'+`i')/2 if `q'_55max==`j'
		
		
		}

		
		local prevValue = `i'

	}


}





*****************************************
*****  STANDARD MPL QUESTIONS     *******
*****************************************


local allquestions "q1 q2 q3 q4 q5 q6 q7"


* This code generates the switching row and the $ value of the switching point
 
 
 

* This procedure generates the following variables 

* `q'_switchRow : first row right hand side is selected, defined only if monotone
* `q'_dominated : 1 if dominated choices, 0 otherwise
* `q'_nonMonotone : 1 if nonMonotone MPL, 0 otherwise

* `q'_nonMonotoneRangeMin : first time the agent choose 2 after chosing 1 (or the first line if the first choice is 2); defined only if nonMonotone =1
* `q'_nonMonotoneRangeMax : last time the agent choose 2 after chosing 1; defined only if nonMonotone =1

* Notice that if the agent chooses always 1, all variables above are undefined except for `q'_dominated = 1


 
* ----- IDENTIFY SWITCHING ROW   ----

* First, we create a variable that identifies the switching row and marks if one answer is not monotone and/or dominated 

 
foreach q of local allquestions {

	* set the correct number of rows per question

	if  "`q'" =="q1" local nrows "17"
	if  "`q'" =="q2" local nrows "17"
	if  "`q'" =="q3" local nrows "17"
	if  "`q'" =="q4" local nrows "17"
	if  "`q'" =="q5" local nrows "19"
	if  "`q'" =="q6" local nrows "23"
	if  "`q'" =="q7" local nrows "17"
	
	gen `q'_switchRow=.
	
	gen `q'_dominated=0
	gen `q'_nonMonotone=0

	gen `q'_nonMonotoneRangeMin=.
	gen `q'_nonMonotoneRangeMax=.

	
	* Dominated choices:

		* Mark as dominated if the choice in row 1 is not equal to 1 for questions 1-6
		if "`q'" != "q7" replace `q'_dominated = 1 if choice`q'1 != 1

		* Or if the choice in the last row is dominated for questions 5 and 7
		if "`q'" == "q5" | "`q'" == "q7" replace `q'_dominated = 1 if choice`q'`nrows'!= 2

	
	gen choice`q'0 = 1
	

	
	forvalue i =1(1)`nrows'{ 

		local j = `i' -1
		
		* For each row, generate the Max if the Min existed and we observe 2 here and 1 in the previous row; 
		* if the Min didn't exist, then generate the Min
		* Since at row 0 the previous row doesn't exist, we have created it.
		
		replace `q'_nonMonotoneRangeMax = `i' if choice`q'`i' == 2 & choice`q'`j' == 1 & `q'_nonMonotoneRangeMin !=. 
		replace `q'_nonMonotoneRangeMin = `i' if choice`q'`i' == 2 & choice`q'`j' == 1 & `q'_nonMonotoneRangeMin ==.
		

	}

	* If we have a Max, we must be nonMonotone; in that case mark it
	
	replace `q'_nonMonotone = 1 if `q'_nonMonotoneRangeMax !=.
	
	* If the Max doesn't exist, we must be not nonMonotone; in that case define the switch and set the Min=.

	
	replace `q'_switchRow=`q'_nonMonotoneRangeMin if `q'_nonMonotoneRangeMax ==.

	replace `q'_nonMonotoneRangeMin =. if `q'_nonMonotoneRangeMax ==.
	
	* Set the switching row and all others to =. if dominated
	
	replace `q'_nonMonotoneRangeMin =. if `q'_dominated ==1
	replace `q'_nonMonotoneRangeMax =. if `q'_dominated ==1
	replace `q'_switchRow =. if `q'_dominated ==1

}



* ----- CONVERT TO DOLLAR VALUES   ----

* Now we convert all this to $$ values. We have to set the grid correctly *


foreach q of local allquestions {

	gen `q'_swDollAv=.
	gen `q'_swDollTop=.
	gen `q'_swDollBottom=.

	gen `q'_swDollAvNonMonMin=.
	gen `q'_swDollAvNonMonMax=.
	gen `q'_swDollTopNonMonMin=.
	gen `q'_swDollTopNonMonMax=.
	gen `q'_swDollBottNonMonMin=.
	gen `q'_swDollBottNonMonMax=.
	
	* We identify the dollar grid per question
	
	if  "`q'" =="q1" local dollarGrid "16 18 18.50 19 19.50 20 20.50 21 21.50 22 22.50 23 23.50 24 24.50 25 27"
	if  "`q'" =="q2" local dollarGrid "16 18 18.50 19 19.50 20 20.50 21 21.50 22 22.50 23 23.50 24 24.50 25 27"
	if  "`q'" =="q3" local dollarGrid "14 16 16.50 17 17.50 18 18.50 19 19.50 20 20.50 21 21.50 22 22.50 23 25"
	if  "`q'" =="q4" local dollarGrid "14 16 16.50 17 17.50 18 18.50 19 19.50 20 20.50 21 21.50 22 22.50 23 25"
	if  "`q'" =="q5" local dollarGrid "0 4 6 7 7.5 8 8.5 9 9.5 10 10.5 11 11.5 12 12.5 13 14 16 20"
	if  "`q'" =="q6" local dollarGrid "18 21 24 27 30 33 33.50 34 34.50 35 35.50 36 36.50 37 37.50 38 38.50 39 42 45 48 51 54"
	if  "`q'" =="q7" local dollarGrid "14 14.5 15 15.5 16 16.5 17 17.5 18 18.5 19 19.5 20 20.5 21 21.5 22"
	
	
	local j = 0
	
	foreach i of local dollarGrid { 

	
		* i denotes the dollar value at that row; j is the counter that indicates the row#.
		*We store the previous value of i in prevValue; this is needed to compute the average.
		
		*If you switch at row j, acquire the value i as top value, the value prevValue for your bottom, and the average for average;
		
		*This is the rule unless you switch already in the first row. In that case we set all the values at the value of the first row.
			
			
			
		local j = `j'+1
		
		if `j'==1 {
		
			replace `q'_swDollBottom= `i' if `q'_switchRow==`j'
			replace `q'_swDollTop= `i' if `q'_switchRow==`j'
			replace `q'_swDollAv= `i' if `q'_switchRow==`j'
			
			replace `q'_swDollBottNonMonMin= `i' if `q'_nonMonotoneRangeMin==`j'
			replace `q'_swDollTopNonMonMin= `i' if `q'_nonMonotoneRangeMin==`j'
			replace `q'_swDollAvNonMonMin= `i' if `q'_nonMonotoneRangeMin==`j'
			
			replace `q'_swDollBottNonMonMax= `i' if `q'_nonMonotoneRangeMax==`j'
			replace `q'_swDollTopNonMonMax= `i' if `q'_nonMonotoneRangeMax==`j'
			replace `q'_swDollAvNonMonMax= `i' if `q'_nonMonotoneRangeMax==`j'
						
		}
		
		
		if `j' > 1 {
		
			replace `q'_swDollBottom= `prevValue' if `q'_switchRow==`j'
			replace `q'_swDollTop= `i' if `q'_switchRow==`j'
			replace `q'_swDollAv= (`prevValue'+`i')/2 if `q'_switchRow==`j'
			
			replace `q'_swDollBottNonMonMin= `prevValue' if `q'_nonMonotoneRangeMin==`j'
			replace `q'_swDollTopNonMonMin= `i' if `q'_nonMonotoneRangeMin==`j'
			replace `q'_swDollAvNonMonMin= (`prevValue'+`i')/2 if `q'_nonMonotoneRangeMin==`j'
			
			replace `q'_swDollBottNonMonMax= `prevValue' if `q'_nonMonotoneRangeMax==`j'
			replace `q'_swDollTopNonMonMax= `i' if `q'_nonMonotoneRangeMax==`j'
			replace `q'_swDollAvNonMonMax= (`prevValue'+`i')/2 if `q'_nonMonotoneRangeMax==`j'
		
		
		}
		
		
		local prevValue = `i'

	}


}



save "data.dta", replace
